package io.xccit.aischool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xccit.aischool.entity.Admin;
import io.xccit.aischool.entity.LoginForm;
import io.xccit.aischool.entity.Student;
import io.xccit.aischool.mapper.IAdminMapper;
import io.xccit.aischool.mapper.IStudentMapper;
import io.xccit.aischool.service.IAdminService;
import io.xccit.aischool.service.IStudentService;
import io.xccit.aischool.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author CH_ywx
 * @date 2023-06-06
 * @description
 */
@Service("adminService")
@Transactional
public class AdminServiceImpl extends ServiceImpl<IAdminMapper, Admin> implements IAdminService {

    @Autowired
    private IAdminMapper adminMapper;
    @Override
    public Admin login(LoginForm loginForm) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        return adminMapper.selectOne(queryWrapper);
    }

    @Override
    public Admin getUserByID(Long userId) {
        return adminMapper.selectById(userId);
    }

    @Override
    public Page<Admin> getAdminPage(Page<Admin> adminPage, String adminName) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(adminName)){
            queryWrapper.like("name",adminName);
        }
        return adminMapper.selectPage(adminPage,queryWrapper);
    }
}
